/*
    This file is part of open-wars <http://code.google.com/p/open-wars/>.

    open-wars is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    open-wars is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with open-wars.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#ifdef WIN32
#define PACKAGE_STRING "open-wars 0.1"
#define PKGDATADIR "media/"
#else
#include <config.h>
#endif

#include <renderer-interface.h>
#include <ui-interface.h>
#include <input-interface.h>
#include <script-interface.h>

#include <opengl-renderer.h>
#include <opengl-input.h>
#include <opengl-ui.h>
#include <lua-script.h>

IRendererInterface *renderer;
IInputInterface *input;
IUIInterface *ui;
IScriptInterface *script;

TEXTURE t = NULL;
TEXTURE cursor = NULL;
FONT f, f2 = NULL;

static void onDraw(boost::any data)
{
	IRendererResResult res = renderer->GetWindowResolution();

	float texHeight = (float)res->height;
	float texWidth = texHeight * 1.5f;

	renderer->DrawTexture(t, (float)(res->width / 2) - (float)(texWidth / 2), 0, (int)texWidth, (int)texHeight);
	renderer->DrawTexture(cursor, 50, 50, 32, 32);
	renderer->DrawFont(f, 200, 500, 255, 255, 155, 255, std::string("Variable-width font support!"));
	renderer->DrawFont(f2, 200, 550, 255, 255, 255, 255, std::string("Sample Heading"));
	renderer->DrawFont(f2, 200, 552, 200, 200, 200, 128, std::string("Sample Heading"));

    ui->Render();
}

static void ScriptOnSetTitle(boost::any data)
{
	renderer->SetWindowCaption(boost::any_cast<char*>(data));
}

static void ScriptOnSetCurrentMouse(boost::any data)
{
	ui->SetCurrentMouse(boost::any_cast<MouseType::Type>(data));
}

int main (int argc, char** argv)
{
	if (argc < 2)
	{
		printf("You must specify the initialization script...\n");
		return 1;
	}

#ifndef WIN32
    printf( PACKAGE_STRING
    		"\nMultmedia located at " PKGDATADIR
            "\nReport bugs to <" PACKAG	qatazgcE_BUGREPORT ">"
            "\n");
#endif
    renderer = new OpenGLRenderer(argc, argv);
    renderer->SetOnWindowDraw(onDraw);
    renderer->InitializeWindow(1024, 768, 32);
    renderer->SetWindowCaption(PACKAGE_STRING);

    input = new OpenGLInput();
    input->Initialize();

    ui = new OpenGLUI();
    ui->SetInputInterface(input);
    ui->SetRenderer(renderer);

    InputInterfaceMouse waitCursor;
    waitCursor.animationType = MouseAnimation::Loop;
    waitCursor.mouseType = MouseType::Busy;
    waitCursor.textures.clear();
    waitCursor.ticksPerFrame = 50;
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy0.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy1.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy2.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy3.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy4.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy5.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy6.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy7.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy8.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy9.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy10.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy11.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy12.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy13.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy14.png")));
    waitCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Busy15.png")));
    ui->AddInterfaceMouse(waitCursor);

    InputInterfaceMouse arrowCursor;
    arrowCursor.animationType = MouseAnimation::Bounce;
    arrowCursor.mouseType = MouseType::Arrow;
    arrowCursor.textures.clear();
    arrowCursor.ticksPerFrame = 50;
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-0.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-1.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-2.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-3.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-4.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-5.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-6.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-7.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-8.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-9.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-10.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-11.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-12.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-13.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-14.png")));
    arrowCursor.textures.push_back(renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass-15.png")));
    ui->AddInterfaceMouse(arrowCursor);


    ui->SetCurrentMouse(MouseType::Arrow);

	t = renderer->LoadTextureFromFile(std::string(PKGDATADIR "openwars-title.png"));
	cursor = renderer->LoadTextureFromFile(std::string(PKGDATADIR "cursor/Pulse_Glass_Unavailable.png"));
    f = renderer->LoadFont(std::string(PKGDATADIR "aotc.ttf"), 18);
    f2 = renderer->LoadFont(std::string(PKGDATADIR "novamono.ttf"), 24);

    script = new LuaScript();
    script->Initialize();
    script->AddOnSetTitle(ScriptOnSetTitle);
	script->AddOnSetCurrentMouse(ScriptOnSetCurrentMouse);
    script->LoadScript(argv[1]);
    glutMainLoop();
    script->Finalize();

    delete renderer;
    return 0;
}
